<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\common\service;

use app\admin\model\SystemWebsite;
use app\common\constants\MenuConstant;
use broadcast\redis\setRedis;
use app\admin\model\WebsiteDefailt;
use think\facade\Db;

class MenuService
{

    /**
     * 管理员ID
     * @var integer
     */
    protected $adminId;

    public function __construct($adminId)
    {
        $this->adminId = $adminId;
        return $this;
    }

    /**
     * 获取首页信息
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getHomeInfo()
    {
        //判断权限   start  
        $low_groupList = setRedis::getCache(session("website"),"default");
        if ($low_groupList) {
            $groupList = $low_groupList;
        }else {
            $groupList = WebsiteDefailt::where('website',session("website"))->find();
            if ($groupList) $groupList = $groupList->toArray();
            setRedis::setCache(session('website'),"default",$groupList,0);
        }
        $notIdList = array();
        if (isset($groupList['configure'])) {
            $group = json_decode($groupList['configure'],true);
            if ($group['whether_teacher'] != 1 && $group['whether_lecturer_ranking'] != 1) {
                $notIdList[] = 6;
            }
            if ($group['whether_curriculum'] != 1) {
                $notIdList[] = 18;
            }
            if ($group['whether_previous'] != 1) {
                $notIdList[] = 21;
            }
            if ($group['whether_individual_stocks'] != 1) {
                $notIdList[] = 14;
            }
            if ($group['whether_market_review'] != 1) {
                $notIdList[] = 20;
            }

            if ($group['whether_index_download'] != 1) {
                $notIdList[] = 22;
            }
            if ($group['whether_advanced_courseware'] != 1) {
                $notIdList[] = 13;
            }
            if ($group['whether_warehouse_reminder'] != 1) {
                $notIdList[] = 16;
            }
            if ($group['whether_closing_reminder'] != 1) {
                $notIdList[] = 17;
            }
            if (isset($group['whether_envelopes'])) {

                if ( $group['whether_envelopes']!=1) {
                    $notIdList[] = 33;
                    $notIdList[] = 34;
                }
                
            }else{
                $notIdList[] = 33;
                $notIdList[] = 34;
            }

            if (!isset($group['whether_luck_draw_function'])) {
                $notIdList[] = 36;
            }else{
                if ($group['whether_luck_draw_function'] != 1) {
                    $notIdList[] = 36;
                }
            }

            if (!isset($group['whether_gift_function'])) {
                $notIdList[] = 40;
            }else{
                if ($group['whether_gift_function'] != 1) {
                    $notIdList[] = 40;
                }
            }
        }
        
        //判断权限   end    
        $data = Db::connect(session("website"))->name('user_rule')
            ->field('title,icon,href')
            ->where("delete_time is null")
            ->where('pid', MenuConstant::HOME_PID)
            ->where("id","not in",implode(',',$notIdList))
            ->find();
        !empty($data) && $data['href'] = __url($data['href']);
        return $data;
    }

    /**
     * 获取后台菜单树信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuTree()
    {
        $menuTreeList = $this->buildMenuChild(0, $this->getMenuData());
        return $menuTreeList;
    }

    private function buildMenuChild($pid, $menuList)
    {
        $treeList = [];
        $authServer = (new AuthService($this->adminId));
        foreach ($menuList as &$v) {
            $check = empty($v['href']) ? true : $authServer->checkNode($v['href']);
            !empty($v['href']) && $v['href'] = __url($v['href']);
            if ($pid == $v['pid'] && $check) {
                $node = $v;
                $child = $this->buildMenuChild($v['id'], $menuList);
                if (!empty($child)) {
                    $node['child'] = $child;
                }
                if (!empty($v['href']) || !empty($child)) {
                    $treeList[] = $node;
                }
            }
        }
        return $treeList;
    }

    /**
     * 获取所有菜单数据
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getMenuData()
    {
        //判断权限   start  
        $low_groupList = setRedis::getCache(session("website"),"default");
        if ($low_groupList) {
            $groupList = $low_groupList;
        }else {
            $groupList = WebsiteDefailt::where('website',session("website"))->find();
            if ($groupList) $groupList = $groupList->toArray();
            setRedis::setCache(session('website'),"default",$groupList,0);
        }
        $notIdList = array();
        if (isset($groupList['configure'])) {
            $group = json_decode($groupList['configure'],true);
            if ($group['whether_teacher'] != 1 && $group['whether_lecturer_ranking'] != 1) {
                $notIdList[] = 6;
            }
            if ($group['whether_curriculum'] != 1) {
                $notIdList[] = 18;
            }
            if ($group['whether_previous'] != 1) {
                $notIdList[] = 21;
            }
            if ($group['whether_individual_stocks'] != 1) {
                $notIdList[] = 14;
            }
            if ($group['whether_market_review'] != 1) {
                $notIdList[] = 20;
            }

            if ($group['whether_index_download'] != 1) {
                $notIdList[] = 22;
            }
            if ($group['whether_advanced_courseware'] != 1) {
                $notIdList[] = 13;
            }
            if ($group['whether_warehouse_reminder'] != 1) {
                $notIdList[] = 16;
            }
            if ($group['whether_closing_reminder'] != 1) {
                $notIdList[] = 17;
            }
            if (isset($group['whether_envelopes'])) {

                if ( $group['whether_envelopes']!=1) {
                    $notIdList[] = 33;
                    $notIdList[] = 34;
                }
                
            }else{
                $notIdList[] = 33;
                $notIdList[] = 34;
            }

            if (!isset($group['whether_luck_draw_function'])) {
                $notIdList[] = 36;
            }else{
                if ($group['whether_luck_draw_function'] != 1) {
                    $notIdList[] = 36;
                }
            }

            if (!isset($group['whether_gift_function'])) {
                $notIdList[] = 40;
            }else{
                if ($group['whether_gift_function'] != 1) {
                    $notIdList[] = 40;
                }
            }
        }
        $rules = Db::connect(session("website"))->name('user_group')
        ->where("id",session("admin.group"))->value("rules");


        $ruleList = explode(",",$rules);

        if  (in_array('4',$ruleList)) {
            $ruleList[] = 25;
            $ruleList[] = 26;
        }
        if (in_array('15',$ruleList)) {
            $ruleList[] = 16;
            $ruleList[] = 17;
        }
        $rules = implode(",",$ruleList);
        //判断权限   end    
        $menuData = Db::connect(session("website"))->name('user_rule')
            ->field('id,pid,title,icon,href,target')
            ->where("delete_time is null")
            ->where([
                ['status', '=', '1'],
                ['pid', '<>', MenuConstant::HOME_PID],
                ['id',"not in",$notIdList]
            ])
            ->where("id",'in',$rules)
            ->order([
                'sort' => 'desc',
                'id'   => 'asc',
            ])
            ->select();
        return $menuData;
    }



    /**
     * 获取首页信息
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getadminInfo()
    {
        //判断权限   start  
        $low_groupList = setRedis::getCache(session("website"),"default");
        if ($low_groupList) {
            $groupList = $low_groupList;
        }else {
            $groupList = WebsiteDefailt::where('website',session("website"))->find();
            if ($groupList) $groupList->toArray();
            setRedis::setCache(session('website'),"default",$groupList,0);
        }
        $notIdList = array();
        if (isset($groupList['configure'])) {
            $group = json_decode($groupList['configure'],true);
            if ($group['whether_teacher'] != 1 && $group['whether_lecturer_ranking'] != 1) {
                $notIdList[] = 6;
            }
            if ($group['whether_curriculum'] != 1) {
                $notIdList[] = 18;
            }
            if ($group['whether_previous'] != 1) {
                $notIdList[] = 21;
            }
            if ($group['whether_individual_stocks'] != 1) {
                $notIdList[] = 14;
            }
            if ($group['whether_market_review'] != 1) {
                $notIdList[] = 20;
            }

            if ($group['whether_index_download'] != 1) {
                $notIdList[] = 22;
            }
            if ($group['whether_advanced_courseware'] != 1) {
                $notIdList[] = 13;
            }
            if ($group['whether_warehouse_reminder'] != 1) {
                $notIdList[] = 16;
            }
            if ($group['whether_closing_reminder'] != 1) {
                $notIdList[] = 17;
            }

            if (isset($group['whether_envelopes'])) {

                if ( $group['whether_envelopes']!=1) {
                    $notIdList[] = 33;
                    $notIdList[] = 34;
                }
                
            }else{
                $notIdList[] = 33;
                $notIdList[] = 34;
            }

            if (!isset($group['whether_luck_draw_function'])) {
                $notIdList[] = 36;
            }else{
                if ($group['whether_luck_draw_function'] != 1) {
                    $notIdList[] = 36;
                }
            }

            if (!isset($group['whether_gift_function'])) {
                $notIdList[] = 40;
            }else{
                if ($group['whether_gift_function'] != 1) {
                    $notIdList[] = 40;
                }
            }
        }
        //判断权限   end    
        $data =Db::connect(session("website"))->name('user_rule')
            ->field('title,icon,href')
            ->where("delete_time is null")
            ->where('pid', MenuConstant::HOME_PID)
            ->where("id","not in",$notIdList)
            ->find();
        !empty($data) && $data['href'] = __url($data['href']);
        return $data;
    }

    /**
     * 获取所有菜单数据
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getadminMenuData()
    {

        //判断权限   start  
        $low_groupList = setRedis::getCache(session("website"),"default");
        if ($low_groupList) {
            $groupList = $low_groupList;
        }else {
            $groupList = WebsiteDefailt::where('website',session("website"))->find();
            if ($groupList) $groupList->toArray();
            setRedis::setCache(session('website'),"default",$groupList,0);
        }
        $notIdList = array();
        if (isset($groupList['configure'])) {
            $group = json_decode($groupList['configure'],true);
            if ($group['whether_teacher'] != 1 && $group['whether_lecturer_ranking'] != 1) {
                $notIdList[] = 6;
            }
            if ($group['whether_curriculum'] != 1) {
                $notIdList[] = 18;
            }
            if ($group['whether_previous'] != 1) {
                $notIdList[] = 21;
            }
            if ($group['whether_individual_stocks'] != 1) {
                $notIdList[] = 14;
            }
            if ($group['whether_market_review'] != 1) {
                $notIdList[] = 20;
            }

            if ($group['whether_index_download'] != 1) {
                $notIdList[] = 22;
            }
            if ($group['whether_advanced_courseware'] != 1) {
                $notIdList[] = 13;
            }
            if ($group['whether_warehouse_reminder'] != 1) {
                $notIdList[] = 16;
            }
            if ($group['whether_closing_reminder'] != 1) {
                $notIdList[] = 17;
            }

            if (isset($group['whether_envelopes'])) {

                if ( $group['whether_envelopes']!=1) {
                    $notIdList[] = 33;
                    $notIdList[] = 34;
                }
                
            }else{
                $notIdList[] = 33;
                $notIdList[] = 34;
            }

            if (!isset($group['whether_luck_draw_function'])) {
                $notIdList[] = 36;
            }else{
                if ($group['whether_luck_draw_function'] != 1) {
                    $notIdList[] = 36;
                }
            }

            if (!isset($group['whether_gift_function'])) {
                $notIdList[] = 40;
            }else{
                if ($group['whether_gift_function'] != 1) {
                    $notIdList[] = 40;
                }
            }
        }
        //判断权限   end    

        $menuData =Db::connect(session("website"))->name('user_rule')
            ->field('id,pid,title,icon,href,target')
            ->where("delete_time is null")
            ->where([
                ['status', '=', '1'],
                ['pid', '<>', MenuConstant::HOME_PID],
                ['id','not in',$notIdList]
            ])
            ->order([
                'sort' => 'desc',
                'id'   => 'asc',
            ])
            ->select();
        return $menuData;
    }
    /**
     * 获取后台菜单树信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getadminMenuTree()
    {
        $menuTreeList = $this->buildMenuChild(0, $this->getadminMenuData());
        return $menuTreeList;
    }

}